package com.fifth.emp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Message;
import com.fifth.common.pojo.entity.Permission;
import com.fifth.common.pojo.entity.Role;
import com.fifth.emp.feign.PermissionFeign;

import com.fifth.emp.mapper.RoleMapper;
import com.fifth.emp.mapper.RoleMapperVo;
import com.fifth.emp.service.IRoleService;
import com.fifth.emp.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 徐志豪
 * @since 2022-09-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private PermissionFeign permissionFeign;
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMapperVo roleMapperVo;

    /**
     * 模糊名字分页查询
     * @param currentPage
     * @param pageSize
     * @param role
     * @return
     */
    @Override
    public CommonResult getRole(Integer currentPage, Integer pageSize,Role role) {
        IPage<Role> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(role.getRoleName())){
            wrapper.like("role_name",role.getRoleName());
        }
        if (StringUtils.hasText(role.getStatus())){
            wrapper.like("status",role.getStatus());
        }
        IPage<Role> page1 = roleMapper.selectRole(page,wrapper);
        System.out.println("总页码:" + page.getPages());
        System.out.println("总条数:" + page.getTotal());
        System.out.println("当前页记录:" + page.getRecords());
        return new CommonResult(2000,"查询成功",page1);
    }

    /**
     * 修改角色状态
     * @param
     * @return
     */
    @Override
    public CommonResult upStatus(String id,String status) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Role role = new Role();
        role.setStatus(status);
        int row = roleMapper.update(role, wrapper);
        return new CommonResult(2000,"修改状态成功",row);
    }

    /**
     * 增加角色及权限
     * @param
     * @return
     */
    @Override
    public CommonResult addRole(Role role) {
        int row = roleMapper.insert(role);
//        QueryWrapper<RoleVo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("role_name",roleVo.getRoleName());
//        RoleVo roleVo1 = roleMapperVo.selectOne(queryWrapper);
//        String id = roleVo1.getId();
//
//        int i = roleMapperVo.insertPermission(roleVo.getPermissionId(),id);

        return new CommonResult(2000,"增加角色成功",row);
    }

    @Override
    public CommonResult deleteRole(String id) {
        int i = roleMapper.deleteById(id);
        if (i!=0){
            return new CommonResult(2000,"删除成功",i);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }

    }

    @Override
    public CommonResult selectRoleAll() {
//        QueryWrapper<Role> wrapper = new QueryWrapper<>();
//        wrapper.eq("is_deleted",0);
//        List<Role> roles = roleMapper.selectList(wrapper);

        List<Role> roles = roleMapper.selectRoleAll();
        return new CommonResult(2000,"查询成功",roles);
    }


    //根据角色id查询当前角色具有的权限和所有权限
    @Override
    public CommonResult selectPermission(String roleId) {
        //所有的权限
        List<Permission> permissionList = permissionFeign.selectAllPermission();
        System.out.println(permissionList+"-----");
        //设置层级关系
        //一级菜单
        List<Permission> firstMenus = new ArrayList<>();
        for (Permission first : permissionList) {
            if (first.getPid().equals("0")) {
                firstMenus.add(first);
            }
        }
        //为一级菜单设置二级菜单
        for (Permission first : firstMenus) {
            //根据一级菜单id 查询该菜单的二级菜单
            first.setChildren(findChildren(permissionList, first.getId()));
        }
        //根据角色id查询当前角色具有的权限
        List<Permission> permission = roleMapper.selectPermissionByRoleId(roleId);
        Map map=new HashMap();
        map.put("firstMenus",firstMenus);
        map.put("permission",permission);
        return new CommonResult(2000,"查询查询成功",map);
    }

    @Override
    public CommonResult updateRole(Role role) {
        int i = roleMapper.updateById(role);
        return new CommonResult(2000,"修改成功",i);
    }

    //方法递归
    private List<Permission> findChildren (List < Permission > permissionList, String id){
        List<Permission> children = new ArrayList<>();
        for (Permission p : permissionList) {
            if (p.getPid().equals(id)) {
                children.add(p);
            }
        }
        for (Permission child : children) {
            child.setChildren(findChildren(permissionList, child.getId()));
        }
        return children;
    }

}
